package com.linkstec.mot.util;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.linkstec.mot.SysConst;

/**
 * 
 * 默认mot-db-setting.properties配置文件
 * 
 * @author PENYAO
 * 
 */
public class ConfigUtil {

	private static Logger logger = LoggerFactory.getLogger(ConfigUtil.class);

	private static Map<String, String> configMsg = null;

	static {
		configMsg = getConfigMap("mot-db-setting");
		logger.info(configMsg.toString());
	}

	public static Map<String, String> getConfigMap(String configFilePath) {
		if (StringUtil.isNullOrEmpty(configFilePath)) {
			return null;
		}
		int dotIndex = configFilePath.lastIndexOf(SysConst.DOT);
		if (dotIndex > 0) {
			configFilePath = new String(configFilePath.substring(0, dotIndex));
		}
		ResourceBundle rb = ResourceBundle.getBundle(configFilePath);
		Map<String, String> result = new HashMap<String, String>();
		if (null != rb) {
			Enumeration<String> configEnum = rb.getKeys();
			while (configEnum.hasMoreElements()) {
				String key = configEnum.nextElement();
				result.put(key, rb.getString(key));
			}
		}
		return result;
	}

	/**
	 * 获取appconfig中配置信息 不带默认值
	 * 
	 * @param key
	 * @return
	 */
	public static String getProperty(String key) {
		return getProperty(key, null);
	}

	/**
	 * 获取appconfig中配置信息 带默认值
	 * 
	 * @param key
	 * @param defaultVal
	 * @return
	 */
	public static String getProperty(String key, String defaultVal) {
		return configMsg.get(key) == null || configMsg.get(key).length() == 0 ? defaultVal
				: configMsg.get(key);
	}
}
